/**
 * 余票管理管理初始化
 */
var TicketRemainder = {
    id: "TicketRemainderTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TicketRemainder.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '调度车辆表id', field: 'staId', visible: false, align: 'center', valign: 'middle'},
        {title: '车次', field: 'trainNo', visible: true, align: 'center', valign: 'middle'},
        {title: '始发站', field: 'departure', visible: true, align: 'center', valign: 'middle'},
        {title: '发车时间', field: 'startTime', visible: true, align: 'center', valign: 'middle'},
        {title: '到达站', field: 'arrive', visible: true, align: 'center', valign: 'middle'},
        {title: '到达时间', field: 'arrTime', visible: true, align: 'center', valign: 'middle'},
        {title: '余票数量', field: 'ticketCount', visible: true, align: 'center', valign: 'middle'}/*,
        {title: '席别详情', field: 'seatDetail', visible: true, align: 'center', valign: 'middle'}
        ,
        {title: '站点顺序', field: 'stationOrder', visible: true, align: 'center', valign: 'middle'}*/
    ];
};

/**
 * 检查是否选中
 */
TicketRemainder.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        TicketRemainder.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加余票管理
 */
TicketRemainder.openAddTicketRemainder = function () {
    var index = layer.open({
        type: 2,
        title: '添加余票管理',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/ticketRemainder/ticketRemainder_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看座位详情
 */
TicketRemainder.openTicketRemainderSeatDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '座位详情',
            area: ['100%', '99%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/ticketRemainder/ticketRemainder_seat_detail/' + TicketRemainder.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 打开查看线路详情
 */
TicketRemainder.openTicketRemainderStationDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '线路详情',
            area: ['100%', '99%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/ticketRemainder/ticketRemainder_station_detail/' + TicketRemainder.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除余票管理
 */
TicketRemainder.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/ticketRemainder/delete", function (data) {
            Feng.success("删除成功!");
            TicketRemainder.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ticketRemainderId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询余票管理列表
 */
TicketRemainder.search = function () {
    var queryData = {};
    queryData['departure'] = $("#departure").val();
    queryData['arrive'] = $("#arrive").val();
    queryData['date'] = $("#date").val();
    TicketRemainder.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = TicketRemainder.initColumn();
    var table = new BSTable(TicketRemainder.id, "/ticketRemainder/list", defaultColunms);
    table.setPaginationType("client");
    TicketRemainder.table = table.init();
    $("#date").val(new Date().Format("yyyy-MM-dd"));
});

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
